package com.ruoyi.web.controller.business.statistics.factory;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.business.goods.domain.JbCommodity;
import com.ruoyi.business.order.domain.JbOrderCommodity;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.controller.business.goods.factory.JbCommodityFactory;
import com.ruoyi.web.controller.business.rule.factory.JbRulesFactory;
import com.ruoyi.web.controller.business.statistics.vo.JbCommissionVO;
import com.ruoyi.web.controller.business.statistics.vo.JbGrossProfitRateQueryVO;
import com.ruoyi.web.controller.business.statistics.vo.JbGrossProfitRateVO;
import com.ruoyi.web.core.JoController;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 业务 订单提成 工厂
 *
 * @author Jyl
 */
@Log4j2
@Component
public class JbCommissionFactory extends JoController
{

    @Autowired
    private JbGrossProfitRateFactory jbGrossProfitRateFactory;
    @Autowired
    private JbCommodityFactory jbCommodityFactory;
    @Autowired
    private JbRulesFactory jbRulesFactory;

    /**
     * 计算 订单提成
     *
     * @param queryVO 毛利率查计算对象
     * @return 提成相关数据
     */
    public JbCommissionVO fnRoyalties(JbGrossProfitRateQueryVO queryVO)
    {
        JbGrossProfitRateVO jbGrossProfitRateVO = jbGrossProfitRateFactory.fnGrossProfitRate(
                queryVO.getList(),
                queryVO.getLogisticsId(),
                queryVO.getAreaCode(),
                queryVO.getOrderTotalAmount()
        );

        BigDecimal orderAmount = jbGrossProfitRateVO.getOrderTotalAmount();//订单金额
        BigDecimal giftAmount = new BigDecimal("0.00");//赠品金额
        BigDecimal premiumPrice = new BigDecimal("0.00");//溢价金额
        BigDecimal goodsAmount = new BigDecimal("0.00");//商品提成

        Long orderId = null;

        // 商品类型金额分组计算
        for (JbOrderCommodity jbOrderCommodity : queryVO.getList())
        {
            if (jbOrderCommodity.getType() < 1)
            {
                premiumPrice = premiumPrice.add(jbOrderCommodity.getUnitPrice()
                        .subtract(jbOrderCommodity.getFactoryPrice())//溢价
                        .multiply(jbOrderCommodity.getQuantity())
                );

                goodsAmount = fn(goodsAmount, jbOrderCommodity);
            } else
                giftAmount = giftAmount.add(jbOrderCommodity.getFactoryPrice().multiply(
                        jbOrderCommodity.getQuantity()));

            orderId = jbOrderCommodity.getOrderId();
        }

        //溢价金额不能低于0
        if (premiumPrice.compareTo(BigDecimal.ZERO) < 0)
        {
            premiumPrice = BigDecimal.ZERO;
        }

        //计算订单提成，溢价分成
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(jbGrossProfitRateVO));
        jsonObject.putAll(jbRulesFactory.fnRoyalties(
                jbGrossProfitRateVO.getInventoryGrossProfitRate(),
                orderAmount,
                goodsAmount,
                premiumPrice,
                giftAmount,
                giftAmount.compareTo(BigDecimal.ZERO) >= 0, //计算是否有赠品
                orderId
        ));

        //格式化数据
        return JSONObject.parseObject(jsonObject.toJSONString(), JbCommissionVO.class);
    }

    /**
     * 计算 商品单独提成系数
     *
     * @param goodsAmount      商品价格合计计算 提成商品提成
     * @param jbOrderCommodity 订货商品对象
     */
    private BigDecimal fn(BigDecimal goodsAmount, JbOrderCommodity jbOrderCommodity)
    {
        //商品价格
        BigDecimal amount = jbOrderCommodity.getUnitPrice()
                .multiply(jbOrderCommodity.getQuantity());
        BigDecimal commissionRatio = jbOrderCommodity.getCommissionRatio();

        //商品提成点 缺省值从商品信息里抓取
        if (ObjectUtil.isEmpty(commissionRatio))
        {
            //商品对象
            JbCommodity jbCommodity = jbCommodityFactory.getInfo(jbOrderCommodity.getCommodityId());
            if (ObjectUtil.isEmpty(jbCommodity.getCommission()))
            {
                throw new ServiceException("商品缺少提成系数，请补全：" + jbCommodity.getCommodityNumber());
            }
            commissionRatio = jbCommodity.getCommission();
        }
        return goodsAmount.add(commissionRatio
                .multiply(amount)
                .divide(BigDecimal.valueOf(100.00), 2, RoundingMode.HALF_UP));
    }
}
